package com.xiao.controller;

import com.github.pagehelper.PageInfo;
import com.xiao.pojo.Books;
import com.xiao.pojo.Borrow;
import com.xiao.pojo.Student;
import com.xiao.service.BookService;
import com.xiao.service.BookServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/book")
public class  BookController {
    @Autowired
    @Qualifier("BookServiceImpl")
private BookService bookService=new BookServiceImpl();

    //查询全部书籍 返回到书籍页面
//    @RequestMapping("/allBook")
//    public String list(Model model){
//        List<Books> list = bookService.queryAllBook();
//
//        model.addAttribute("list",list);
//        return "allBook";
//    }
//    @RequestMapping("/stu_allbook")
//    public String list2(Model model){
//        List<Books> list = bookService.queryAllBook();
//
//        model.addAttribute("list",list);
//        return "stu_allbook";
//    }

    //跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }
//添加书籍请求
    @RequestMapping("/addBook")
    public String addBook(@Param("bookName")String bookName,@Param("bookCounts")Integer bookCounts,@Param("detail")String detail,@Param("pic")String pic){
        Random rd = new Random();
        int bookID = rd.nextInt(10);
        System.out.println(bookID);
        Books books = new Books(bookID,bookName, bookCounts, detail, pic);
        System.out.println(bookName);
        System.out.println(bookCounts);
        System.out.println(detail);
        System.out.println(pic);
        System.out.println("addBook=>"+books);
        bookService.addBook(books);
        return "redirect:/book/admin-findAll";//重定向到我们的@RequestMapping("/allbook")
    }
    //跳转到修改页面
    @RequestMapping("/toUpdate")
    public  String toUpdatePaper(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("QBook",books);
        return "updateBook";
    }
    //修改书籍
    @RequestMapping("/updateBook")
    public String updateBook(Books books){

        bookService.updateBook(books);
        return "redirect:/book/admin-findAll";
    }
    //删除书籍
    @RequestMapping("/deleteBook/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id){
      bookService.deleteBookById(id);
        return "redirect:/book/admin-findAll";
    }
    //学生查询书籍
    @RequestMapping("/queryBook")
    public ModelAndView queryBook(@RequestParam(name = "page",required = true,defaultValue = "1")int page,@RequestParam(name = "size",required = true,defaultValue = "4")int size,String queryBookName,Model model) {
        ModelAndView mv = new ModelAndView();
        Books books = bookService.queryBookByName(queryBookName);

        List<Books> booksList = new ArrayList<Books>();
        booksList.add(books);
        if (books == null) {
            booksList = bookService.queryAllBook(page, size);
            PageInfo pageInfo = new PageInfo(booksList);
            model.addAttribute("error", "未查到");
            mv.addObject("pageInfo", pageInfo);
            mv.setViewName("allbook-page-stu");
            return mv;
        }else {
            PageInfo pageInfo = new PageInfo(booksList);
            mv.addObject("pageInfo", pageInfo);
            mv.setViewName("StuQueryBookSuccess");
            return mv;
        }


    }
    //管理员查询书籍
    @RequestMapping("/admin-queryBook")
    public ModelAndView adminqueryBook(@RequestParam(name = "page",required = true,defaultValue = "1")int page,@RequestParam(name = "size",required = true,defaultValue = "4")int size,String queryBookName,Model model) {
        ModelAndView mv = new ModelAndView();
        Books books = bookService.queryBookByName(queryBookName);

        List<Books> booksList = new ArrayList<Books>();
        booksList.add(books);
        if (books == null) {
            booksList = bookService.queryAllBook(page, size);
            model.addAttribute("error", "未查到");
            PageInfo pageInfo = new PageInfo(booksList);
            mv.addObject("pageInfo", pageInfo);
            mv.setViewName("allBook-page");
return mv;
        }else {
            PageInfo pageInfo = new PageInfo(booksList);
            mv.addObject("pageInfo", pageInfo);
            mv.setViewName("BookQuerySuccess");


            return mv;
        }

    }

//学生分页查询s书籍
@RequestMapping("/findAll")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")int page,@RequestParam(name = "size",required = true,defaultValue = "4")int size)throws Exception{
    ModelAndView mv = new ModelAndView();
    List<Books> booksList = bookService.queryAllBook(page, size);
    PageInfo pageInfo=new PageInfo(booksList);

    mv.addObject("pageInfo",pageInfo);
    mv.setViewName("allbook-page-stu");
    return mv;
}
    //管理员分页查询
    @RequestMapping("/admin-findAll")
    public ModelAndView admin_findAll(@RequestParam(name = "page",required = true,defaultValue = "1")int page,@RequestParam(name = "size",required = true,defaultValue = "4")int size)throws Exception{
        ModelAndView mv = new ModelAndView();
        List<Books> booksList = bookService.queryAllBook(page, size);
        PageInfo pageInfo=new PageInfo(booksList);

        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("allBook-page");
        return mv;
    }
    //借书
    @RequestMapping("/borrow")
    public ModelAndView borrowBook(Model model, int id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        Books Borrow_books = bookService.queryBookById(id);
        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession();
        Student stu = (Student) session.getAttribute("stu");
        int bookID = Borrow_books.getBookID();
        System.out.println(bookID);
        String bookName = Borrow_books.getBookName();
        String sno = stu.getSno();
        String sname = stu.getSname();
        Borrow borrow = new Borrow(bookID, bookName, sno, sname);
        List<Borrow> borrow_list = bookService.queryAllBorrowBySno(sno);

        System.out.println(borrow_list.contains(borrow));
        if (borrow_list.contains(borrow)) {
           model.addAttribute("exit","你已经借过这本书");
mv.setViewName("b-error");
        }
        else {

            bookService.addBorrow(borrow);

            mv.addObject("borrow_list", borrow_list);
           return new ModelAndView("redirect:/book/borrow-list");
        }
return mv;
    }



//查询借阅记录
    @RequestMapping("/borrow-list")
    public ModelAndView borrowList(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession();
        Student stu = (Student) session.getAttribute("stu");
        String sno = stu.getSno();
        List<Borrow> borrow_list = bookService.queryAllBorrowBySno(sno);
        mv.addObject("borrow_list", borrow_list);
        mv.setViewName("b-list");

        return mv;
    }

//归还书籍
@RequestMapping("/borrow-return/{bookID}")

public String ReturnBook(@PathVariable("bookID") int bookID){

bookService.returnBook(bookID);
bookService.increaseBook(bookID);
    return "redirect:/book/borrow-list";
}
//管理员查询所有借阅记录
@RequestMapping("/admin-borrow-list")
public ModelAndView admin_borrowList(HttpServletRequest request) {
    ModelAndView mv = new ModelAndView();


    List<Borrow> borrow_list = bookService.queryAllBorrow();
    mv.addObject("borrow_list", borrow_list);
    mv.setViewName("admin-borrowList");

    return mv;
}
    //管理员来归还书籍
    @RequestMapping("/admin-borrow-return/{bookID}")

    public String admin_ReturnBook(@PathVariable("bookID") int bookID){

        bookService.returnBook(bookID);
        bookService.increaseBook(bookID);
        return "redirect:/book/admin-borrow-list";
    }
  /* RequestMapping @RequestMapping("/queryAppointByAid")
    public String queryAppointByAid(String aid){

    }*/
}
